package hk.m4s.lr.repair.test.ui.baoyang;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.common.zbar.CaptureActivity;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.MaintenanceInfoModel;
import hk.m4s.lr.repair.test.model.MaintenanceInfoModelResult;
import hk.m4s.lr.repair.test.model.MaintenanceModle;
import hk.m4s.lr.repair.test.service.device.DeviceListervice;
import hk.m4s.lr.repair.test.ui.adapter.LookMainListAdapter;
import hk.m4s.lr.repair.test.ui.adapter.LookRepaireListAdapter;
import hk.m4s.lr.repair.test.ui.equipment.LookRepaireDetailActivity;
import hk.m4s.lr.repair.test.ui.equipment.LookRepaireFinishDetailActivity;
import hk.m4s.lr.repair.test.views.InnerGridView;
import hk.m4s.lr.repair.test.views.listview.VListView;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/3/22.
 * 维修任务详情
 */

public class LookRepaireActivity extends UeBaseActivity implements VListView.IXListViewListener {
    private Context context;
    private TextView equ_titlte, tipsText;
    private InnerGridView innerGridView;
    private String type = "", state = "", name = "";
    private List<MaintenanceInfoModel> lists = new ArrayList<>();
    private LookRepaireListAdapter deviceMainListAdapter;
    private VListView vlist;
    private int pageNum = 0;
    private int pageSize = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_look_baoyang_detaile);
        context = this;
        hiddenFooter();
        showGoBackBtn();
        setTitleText("维修列表");
        type = getIntent().getStringExtra("type");
        state = getIntent().getStringExtra("state");
        findBaseView();
    }

    private void findBaseView() {
        equ_titlte = (TextView) findViewById(R.id.equ_titlte);
        tipsText = (TextView) findViewById(R.id.tipsText);
        innerGridView = (InnerGridView) findViewById(R.id.innergrid);
        vlist = (VListView) findViewById(R.id.vlist);
        vlist.setXListViewListener(this);
        vlist.setOnItemClickListener(clickItem);
        deviceMainListAdapter = new LookRepaireListAdapter(context, lists);
        vlist.setAdapter(deviceMainListAdapter);

    }

    private void onLoad() {
        vlist.stopRefresh();
        vlist.stopLoadMore();
        Date data = new Date(System.currentTimeMillis());
        vlist.setRefreshTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.CHINA).format(data));
    }

    @Override
    protected void onResume() {
        super.onResume();
        deviceMaintenanceInfo("1");
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub

        vlist.setPullLoadEnable(true);
        pageNum = 0;
        deviceMaintenanceInfo("1");
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        vlist.setPullRefreshEnable(true);
        pageNum++;
        deviceMaintenanceInfo("2");
    }

    public void deviceMaintenanceInfo(final String refreshType) {
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("device_type", Constant.device_type);
        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
        try {
            JSONObject jsondata = new JSONObject();
            jsondata.put("type", type);
            jsondata.put("state", state);
            parmas.put("jsonText", jsondata.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        DeviceListervice.taskByState(context, parmas, new ResponseCallback<MaintenanceInfoModelResult>() {
            @Override
            public void onSuccess(MaintenanceInfoModelResult accountModel) {
                onLoad();
                if (accountModel.getList() != null) {
                    tipsText.setVisibility(View.GONE);
                    switch (refreshType) {
                        case "1":
                            lists.clear();
                            lists.addAll(accountModel.getList());
                            if (accountModel.getList().size() <= pageSize) {
                                vlist.removeFooterView();
                                vlist.setPullLoadEnable(false);
                            } else {
                                vlist.showFooterView();
                                vlist.setPullLoadEnable(true);
                            }
                            break;
                        case "2":
                            lists.addAll(accountModel.getList());
                            if (accountModel.getList().size() <= pageSize) {
                                vlist.removeFooterView();
                                vlist.setPullLoadEnable(false);
                            }
                            break;
                    }
                    deviceMainListAdapter.notifyDataSetChanged();
                } else {
                    lists.clear();
                    deviceMainListAdapter.notifyDataSetChanged();
                    tipsText.setVisibility(View.VISIBLE);
                    vlist.removeFooterView();
                    vlist.setPullLoadEnable(false);
                }
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }

    AdapterView.OnItemClickListener clickItem = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            // TODO Auto-generated method stub
            MaintenanceInfoModel model = lists.get(arg2 - 1);
            Intent intent = null;
            if (state.equals("1")) {
                intent = new Intent(LookRepaireActivity.this, LookRepaireFinishDetailActivity.class);
            } else {
                intent = new Intent(LookRepaireActivity.this, LookRepaireDetailActivity.class);
            }
            intent.putExtra("model", model);
            intent.putExtra("state", state);
            startActivity(intent);


        }

    };
}
